package com.aixin.project.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aixin.project.data.*;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import com.aixin.project.db.CourseOrderDao;
import com.aixin.project.handler.CourseHandler;
import com.aixin.project.handler.CourseOrderHandler;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.handler.UserHandler;
import com.aixin.project.util.AxMemcached;
import com.aixin.project.util.DateUtil;
import com.aixin.project.util.RequestUtil;

public class OrderpayServlet extends BaseServlet {

    @Override
    protected Template execute(HttpServletRequest request,
                               HttpServletResponse response, Context ctx) {

        AxSession axsession = (AxSession) request.getSession().getAttribute(
                CommonConstants.USER_SESSION);
        if (null == axsession || axsession.getUserType().equals(UserType.EXPERT)) {
            this.sendRedirect(response, "/");
            return null;
        }

        long id = axsession.getUid();
        int cid = RequestUtil.getInt(request, "cid", -1);

        Course course = CourseHandler.find(cid);
        if (course != null && !course.getStatus().equals("已结束")) {
            Expert expert = ExpertHandler.find(course.getExpertid());
            User user = UserHandler.find(id);
            ctx.put("user", user);
            ctx.put("expert", expert);
            ctx.put("course", course);
            double totalPrice = RequestUtil.getDouble(request, "consume", 0.00);
            //	double price = RequestUtil.getDouble(request, "price", 0.00);
            int point = RequestUtil.getInt(request, "courseNum", -1);
            long porderid = RequestUtil.getLong(request, "porderid", -1);
            long editorderid = RequestUtil.getLong(request, "eorderid", -1);
            long courseorderid = -1;
            if (totalPrice != 0 && point != -1) {
                if (editorderid > -1) { // edit order
                    CourseOrder eorder = CourseOrderHandler.find(editorderid);
                    if (eorder != null && eorder.getStatus() == 0) {
                        eorder.setPrice(totalPrice);
                        eorder.setPoint(point);
                        int status = CourseOrderHandler.update(eorder);
                        if(status == 1){
                            courseorderid = eorder.getId();
                            ctx.put("totalPrice", totalPrice);
                            ctx.put("point", point);
                            ctx.put("courseorderid", courseorderid);
                            AxMemcached.delete(CommonConstants.MKEY_COURSEORDER_LIST+id);
                        }else{
                            ctx.put("error","订单更新失败！");
                        }
                    } else { //create new order
                        ctx.put("error","订单更新失败！");
                    }
                } else { // create new order
                    CourseOrder order = new CourseOrder();
                    order.setPorderid(porderid);
                    order.setCourseid(cid);
                    order.setExportid(expert.getId());
                    order.setPoint(point);
                    order.setPrice(totalPrice);
                    order.setUserid(id);
                    long orderid = CourseOrderHandler.create(order);
                    if (orderid > 0) {
                        ctx.put("totalPrice", totalPrice);
                        ctx.put("point", point);
                        ctx.put("courseorderid", orderid);
                        AxMemcached.delete(CommonConstants.MKEY_COURSEORDER_LIST+id);
                    }else{
                        ctx.put("error","订单创建失败！");
                    }
                }
            } else {
                ctx.put("error", "无效操作!");
            }
        } else {
            ctx.put("error", "无效的课程!");
        }
        return getTemplate("ordersuc.vm");
    }

}
